import { createBrowserRouter, Navigate } from "react-router-dom";
import Login from "./pages/Login/Login";
import AgentSelection from "./pages/AgentSelection/AgentSelection";
import Chat from "./pages/Chat/Chat";
import Profile from "./pages/Profile/Profile";
import MainLayout from "./components/Layout/MainLayout";

// 身份验证守卫
const AuthGuard = ({ children }: { children: React.ReactNode }) => {
  const isAuthenticated = localStorage.getItem("token") !== null;
  if (!isAuthenticated) {
    return <Navigate to="/login" replace />;
  }
  return <>{children}</>;
};

const router = createBrowserRouter([
  {
    path: "/login",
    element: <Login />,
  },
  {
    path: "/",
    element: (
      <AuthGuard>
        <MainLayout />
      </AuthGuard>
    ),
    children: [
      {
        path: "/",
        element: <Navigate to="/agents" replace />,
      },
      {
        path: "/agents",
        element: <AgentSelection />,
      },
      {
        path: "/chat/:agentId",
        element: <Chat />,
      },
      {
        path: "/profile",
        element: <Profile />,
      },
    ],
  },
  {
    path: "*",
    element: <Navigate to="/" replace />,
  },
]);

export default router;
